package com.erp.app.servlet.web;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.types.DocAttach;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.util.logger.Logger;

public class DocAttachServlet extends HttpServlet {
    final static public String DELEGATE_SESSION_NAME = "SESSION_SM_DELEGATE";
    final static public String ICON_STORE_SESSION_NAME = "ICON_STORE";
    final static public String DOC_STORE_SESSION_NAME = "DOC_STORE";

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Object obj = getDocAttach(request);

            if (obj == null || !(obj instanceof DocAttach)) {// can't find
                                                                // the obj
                                                                // anywhere!
                Logger.debug(this, "ViewImageServlet: can't find any attachment object anywhere!");
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                return;
            }
            DocAttach attachment = (DocAttach) obj;

            OutputStream out = response.getOutputStream();

            String contentType = attachment.getContentType();

            // response.setContentType(vo.getContentType);

            response.setContentType(contentType);
            response.setStatus(response.SC_OK);

            if (attachment.getImage() == null) {                
                Logger.debug(this, "attachment not found at: ");                
            } else {
                out.write(attachment.getImage());
            }

            out.close();
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }

        return;
    }

    protected DocAttach getDocAttach(HttpServletRequest request) {
    	return null;

//        SimpleBaseVo inspectSignVO = null;
//        InspectorSignDetail detailRow = null;
//
//        try {
//            HttpSession session = request.getSession(false);
//            String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
//            String signCode = request.getParameter("signCode");
//            int rowIdx = Integer.parseInt(request.getParameter("attachIdx"));
//
//            InspectorSignsForm form = (InspectorSignsForm) session.getAttribute("inspectorSignsForm");
//            ArrayList rows = form.getMultiRows();
//
//            detailRow = (InspectorSignDetail) rows.get(rowIdx);
//
//        } catch (Exception e) {
//            System.err.println("DocAttachServlet.getDocAttach(): " + e.getMessage());
//            e.printStackTrace();
//        }
//        return (DocAttach) detailRow;
    }

    protected SimpleSearchMaintStyleDelegate getMasterDelegate(HttpSession session) {
        SimpleSearchMaintStyleDelegate dg = null;
        try {
            if (session.getAttribute(DELEGATE_SESSION_NAME) != null) {
                dg = (SimpleSearchMaintStyleDelegate) session.getAttribute(DELEGATE_SESSION_NAME);
            } else {
                dg = new SimpleSearchMaintStyleDelegate();
                session.setAttribute(DELEGATE_SESSION_NAME, dg);
            }
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }
        return dg;
    }

}
